from sklearn import neighbors, datasets, preprocessing
from sklearn.model_selection import train_test_split, cross_val_score
from sklearn.metrics import accuracy_score
#加载数据
iris = datasets.load_iris()
# print(iris)
#划分数据
x, y = iris.data, iris.target
# print(x)
# print(y)

x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.3)

#数据与处理

scaler = preprocessing.StandardScaler().fit(x_train)
x_train = scaler.transform(x_train)
x_test = scaler.transform(x_test)
# print(x_train)
# print(x_test)
#创建模型
knn = neighbors.KNeighborsClassifier(n_neighbors=12)

#模型拟合
knn.fit(x_train, y_train)
score = cross_val_score(knn, x_train, y_train, cv=5, scoring='accuracy')
# print(score)
# print(score.mean())
#预测
y_pred = knn.predict(x_test)
print(accuracy_score(y_test, y_pred))